package pl.edu.agh.climatex.ui;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import pl.edu.agh.climatex.util.Messages;

public class Init {

	public static void main(String[] args) {
		Init init = new Init();
		Display display = new Display();
		Shell shell = init.createAndOpenShell(display);
		init.addControllingComposite(shell);
		init.addInformationComposite(shell);
		init.addMalfunctionStatusComposite(shell);
		shell.layout();
		shell.pack();
		shell.setLocation(init.getCenterLocation(shell));
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private MalfunctionStatusComposite addMalfunctionStatusComposite(Shell shell) {
		MalfunctionStatusComposite composite = new MalfunctionStatusComposite(shell, SWT.NONE);
		composite.setLayoutData(GridDataFactory.fillDefaults().create());
		return composite;		
	}

	private InformationComposite addInformationComposite(Shell shell) {
		InformationComposite composite = new InformationComposite(shell, SWT.NONE);
		composite.setLayoutData(GridDataFactory.fillDefaults().create());
		return composite;
		
	}

	private ControllingComposite addControllingComposite(Shell shell) {
		ControllingComposite composite = new ControllingComposite(shell, SWT.NONE);
		composite.setLayoutData(GridDataFactory.fillDefaults().span(2, 1).create());
		return composite;
	}

	private Shell createAndOpenShell(Display display) {
		Shell shell = new Shell(display, SWT.MIN | SWT.CLOSE | SWT.TITLE | SWT.RESIZE);
		shell.setText(Messages.ShellTitle);
		shell.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).create());
		shell.pack();
		shell.open();

		return shell;
	}

	private Point getCenterLocation(Control control) {
		return new Point(control.getDisplay().getClientArea().width / 2
				- control.getSize().x / 2,
				control.getDisplay().getClientArea().height / 3
						- control.getSize().y / 2);
	}

}
